package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type DeviceEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	Manufacturer string // 设备厂商名称
	DeviceType   string // 设备类型名
	DeviceName   string // 设备名称

	ChannelType string // 通道类型
	ChannelName string // 通道名称

	DeviceParam map[string]interface{} // 配置参数
	ExtendParam map[string]interface{} // 扩展参数
}

type devicePo struct {
	Id           int64                  `json:"id" db:"id"`                     // 对象id
	CreateTime   int64                  `json:"createTime" db:"create_time"`    // 创建时间
	UpdateTime   int64                  `json:"updateTime" db:"update_time"`    // 更新时间
	Manufacturer string                 `json:"manufacturer" db:"manufacturer"` // 设备厂商名称
	DeviceType   string                 `json:"deviceType" db:"device_type"`    // 设备类型名
	DeviceName   string                 `json:"deviceName" db:"device_name"`    // 设备名称
	ChannelType  string                 `json:"channelType" db:"channel_type"`  // 通道类型
	ChannelName  string                 `json:"channelName" db:"channel_name"`  // 通道名称
	DeviceParam  map[string]interface{} `json:"deviceParam" db:"device_param"`  // 配置参数
	ExtendParam  map[string]interface{} `json:"extendParam" db:"extend_param"`  // 扩展参数
}

func (e *DeviceEntity) Type() string {
	return "DeviceEntity"
}

func (e *DeviceEntity) TableName() string {
	return "tb_device"
}

func (e *DeviceEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *DeviceEntity) Instance() *DeviceEntity {
	return &DeviceEntity{
		BaseEntity:  new(commEntity.BaseEntity),
		DeviceParam: make(map[string]interface{}),
		ExtendParam: make(map[string]interface{}),
	}
}

func (e *DeviceEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *DeviceEntity) NewPo() interface{} {
	return &devicePo{
		DeviceParam: make(map[string]interface{}),
		ExtendParam: make(map[string]interface{}),
	}
}

func (e *DeviceEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *DeviceEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.DeviceName)
	return p
}

func (e *DeviceEntity) BuildByJson(jsn *string) error {
	var po devicePo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *DeviceEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *DeviceEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*devicePo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.DeviceParam = po.DeviceParam
	ptr.ExtendParam = po.ExtendParam
	ptr.Manufacturer = po.Manufacturer
	ptr.DeviceType = po.DeviceType
	ptr.DeviceName = po.DeviceName
	ptr.ChannelType = po.ChannelType
	ptr.ChannelName = po.ChannelName
}

func (e *DeviceEntity) BuildToPo() interface{} {
	ptr := e
	po := devicePo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.DeviceParam = ptr.DeviceParam
	po.ExtendParam = ptr.ExtendParam
	po.Manufacturer = ptr.Manufacturer
	po.DeviceType = ptr.DeviceType
	po.DeviceName = ptr.DeviceName
	po.ChannelType = ptr.ChannelType
	po.ChannelName = ptr.ChannelName

	return &po
}
